const base64encodeChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

export function base64encode(str: string) {
	let c1, c2, c3
	let len = str.length
	let i = 0
	let out = ''
	while (i < len) {
		c1 = str.charCodeAt(i++) & 0xff
		if (i === len) {
			out += base64encodeChars.charAt(c1 >> 2)
			out += base64encodeChars.charAt((c1 & 0x3) << 4)
			out += '=='
			break
		}
		c2 = str.charCodeAt(i++)
		if (i === len) {
			out += base64encodeChars.charAt(c1 >> 2)
			out += base64encodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4))
			out += base64encodeChars.charAt((c2 & 0xf) << 2)
			out += '='
			break
		}
		c3 = str.charCodeAt(i++)
		out += base64encodeChars.charAt(c1 >> 2)
		out += base64encodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4))
		out += base64encodeChars.charAt(((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6))
		out += base64encodeChars.charAt(c3 & 0x3f)
	}
	return out
}
export function utf16to8(str: string) {
	let c
	let out = ''
	let len = str.length
	for (let i = 0; i < len; i++) {
		c = str.charCodeAt(i)
		if (c >= 0x0001 && c <= 0x007f) {
			out += str.charAt(i)
		} else if (c > 0x07ff) {
			out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f))
			out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f))
			out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f))
		} else {
			out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f))
			out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f))
		}
	}
	return out
}
export function isIE() {
	if (!!(window as any).ActiveXObject || 'ActiveXObject' in window) {
		return true
	} else {
		return false
	}
}
